package com.project.web.admin.controller.core;

import com.project.common.web.utils.file.FileUtil;
import com.project.model.core.Menu;
import com.project.web.admin.controller.common.BaseController;
import com.project.web.admin.service.core.MenuService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yun.common.utils.appInterface.Message;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("adminMenuController")
@RequestMapping("/admin/menu")
public class MenuController extends BaseController {

    private static final long serialVersionUID = 4290624554576157297L;
    @Resource(name = "menuServiceImpl")
    private MenuService menuService;

    /**
     * 树形页面入口
     */
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    public String tree(Model model) {
        return "/intfc/core/menu/tree.ftl";
    }

    /**
     * 异步获取节点
     */
    @ResponseBody
    @RequestMapping(value = "/getTree", method = RequestMethod.GET)
    public List<Menu> getTree(Long id) {

        if (null != id) {
            List<Menu> childrenNode = menuService.findChildrenNodes(id);
            if (CollectionUtils.isNotEmpty(childrenNode)) {
                return childrenNode;
            }
        }

        return menuService.findOnlyRoots();
    }

    /**
     * 添加
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model) {
        return "/intfc/core/menu/add.ftl";
    }

    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Map<String, Object> save(Menu menu, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        menu.setMedium(FileUtil.copyForPlUpload(request, menu.getMedium(), FileUtil.MENU_IMAGE_DIR));
        menu.setMediumBg(FileUtil.copyForPlUpload(request, menu.getMediumBg(), FileUtil.MENU_IMAGE_DIR));
        menuService.save(menu);
        map.put("success", true);
        map.put("type", Message.Type.success.toString());
        map.put("msg", "保存成功!");
        map.put("data", menu);
        return map;
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, Model model) {
        Menu menu = menuService.getById(id);
        if (menu != null) {
            model.addAttribute("menu", menu);
        }
        return "/intfc/core/menu/edit.ftl";

    }

    /**
     * 更新
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Map<String, Object> update(Menu menu, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Menu temp = menuService.getById(menu.getId());
        FileUtil.updateForUpload(request, temp, menu, FileUtil.MENU_IMAGE_DIR, new String[]{"medium", "mediumBg"});
        menuService.update(menu);
        map.put("success", true);
        map.put("type", Message.Type.success.toString());
        map.put("msg", "保存成功!");
        map.put("data", menu);
        return map;
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Map<String, Object> delete(Long id) {

        Menu menu = menuService.getById(id);
        Map<String, Object> map = new HashMap<String, Object>();
        Menu menuTemp = new Menu();
        menuTemp.setParentId(id);
        List<Menu> menus = menuService.selectList(menuTemp);
        if (CollectionUtils.isNotEmpty(menus)) {
            map.put("success", false);
            map.put("type", Message.Type.warn.toString());
            map.put("msg", "还存在子节点不能删除!");
            return map;
        }
        if (menu != null) {
            menuService.delete(id);
            map.put("success", true);
            map.put("type", Message.Type.success.toString());
            map.put("msg", "删除成功!");
            return map;
        } else {
            map.put("success", false);
            map.put("type", Message.Type.warn.toString());
            map.put("msg", "要操作的对象不存在或已被删除!");
            return map;
        }
    }


}
